<template>
  <div>
    <!-- close:所有的、任意形式的关闭弹窗 -->
    <el-dialog
      :title="info.isAdd ? '添加商品' : '修改商品'"
      :visible.sync="info.isShow"
      width="50%"
      @close="cancel"
    >
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="详细描述" name="first">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="一级分类" prop="first_cateid">
              <el-select
                v-model="ruleForm.first_cateid"
                placeholder="请选择一级分类"
                @change="firstChange"
              >
                <el-option
                  v-for="(item, index) in firstList"
                  :key="index"
                  :label="item.catename"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="二级分类" prop="second_cateid">
              <el-select
                v-model="ruleForm.second_cateid"
                placeholder="请选择二级分类"
              >
                <el-option
                  v-for="(item, index) in secondList"
                  :key="index"
                  :label="item.catename"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="商品名称" prop="goodsname">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
              <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="ruleForm.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="2">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="详细描述" name="second" ref="second">
          <div id="editor"></div>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入负责添加角色的函数
import { getCatelistTree, getSecondCatelist } from "../../request";
// 引入富文本编辑器
import E from 'wangeditor'
// 弹窗的默认值
let defaultData = {
  roleid: 1,
  username: "",
  password: "",
  status: 1,
};
export default {
  props: {
    info: {
      type: Object,
      default: {
        isAdd: false,
        isShow: false,
      },
    },
  },
  data() {
    return {
      activeName: "first",
      // 表单的数据
      ruleForm: { roleid: 3, username: "", password: "", status: 1 },
      // 表单的规则
      rules: {
        roleid: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
        ],
      },
      // 一级分类列表
      firstList: [],
      // 二级分类列表
      secondList: [],
      // 是否已经存在editor
      hasEditor: false,
      editor: {}
    };
  },
  created() {
    // 获取分类列表（一级）
    this.getList();
  },
  mounted() {
  },
  methods: {
    async getList() {
      let res = await getCatelistTree();
      this.firstList = res.list;
    },
    // 一级分类的数据变化时执行的函数
    async firstChange() {
      let id = this.ruleForm.first_cateid;
      let res = await getSecondCatelist(id);
      this.secondList = res.list;
    },
    // 设置当前组件状态
    setStatus(val) {
      this.ruleForm = { ...val };
    },
    // 点击标签页的切换
    handleClick(tab, event) {
      if(tab.name === 'second' && !this.hasEditor) {
        this.editor = new E( document.getElementById('editor') )
        this.editor.create();
        this.hasEditor = true;
        // 设置编辑器内容
        this.editor.txt.html(this.ruleForm.description);
      }
    },
    // 提交表单
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 封装请求
          let res;
          // 获取富文本编辑器中的内容
          if(this.hasEditor) {
            // 有编辑器时使用编辑器中的description
            this.ruleForm.description = this.editor.txt.html();
          }else {
            // 没有就使用空字符串
            this.ruleForm.description = ''
          }
          if (this.info.isAdd) {
            res = userAdd(this.ruleForm);
          } else {
            res = userEdit(this.ruleForm);
          }
          if (res) {
            this.cancel();
            // 兄弟组件传值，告诉list组件更新数据
            this.$bus.$emit("update");
          }
        } else {
          return false;
        }
      });
    },
    // 点击取消，清空数据
    cancel() {
      // 重置弹窗中的数据
      this.ruleForm = { ...defaultData };
      // 关闭弹窗
      this.info.isShow = false;
    },
  },
};
</script>

<style>
#editor {
  width: 100%;
  height: 200px;
}
</style>